<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: blue;
            border: 1px solid black;
            top: 300px;
            left: 300px;

        }
        .box1 {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: olive;
            border: 1px solid black;
            top: 300px;
            left: 600px;
        }
    </style>
</head>

<body> 
    <div id="box"></div>
    <div class="box1"></div>
    <script>
        var box = document.getElementById('box');
        var box1 = document.getElementsByClassName('box1');
        var t = 300, l = 300;
        document.onkeydown = function (e) {
            switch (e.keyCode) {
                case 37: l -= 3; break;
                case 38: t -= 3; break;
                case 39: l += 3; break;
                case 40: t += 3; break;
                case 32: t -= 100; break;
            }
            if(e.keyCode==32){
                box.style.background = "red";
            }
            box.style.left = l + 'px';
            box.style.top = t + 'px';
        }
        box1.onclick = function () {
            box1.style.background = "red";
        }
    </script>
</body>

</html>